﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace Oscilloscope
{
    public static class OscilloscopeHelper
    {
        public static Pen sBlackPen = new Pen(Color.Black, 1.5f);
        public static Pen sRedPen = new Pen(Color.Red);
        public static Pen sBluePen = new Pen(Color.Blue);

        private static float marginLeft     = 0f;
        private static float marginRight    = 1f;
        private static float marginTop      = 0f;
        private static float marginBottom   = 0;
        private static float marginArrow    = 0.010f;

        public static float SignalFunction(float x, float amplitude, float frequence, float phase)
        {
            return (float) (amplitude * Math.Sin(x * frequence * phase));
        }

        public static void DrawAxis(Graphics graph)
        {
            // X axis
            graph.DrawLine(sBlackPen, graph.VisibleClipBounds.Width * marginLeft, graph.VisibleClipBounds.Height * marginTop,
                                        graph.VisibleClipBounds.Width * marginRight, graph.VisibleClipBounds.Height * marginBottom);
            // Arrow
            graph.DrawLine(sBlackPen, graph.VisibleClipBounds.Width * marginLeft, graph.VisibleClipBounds.Height * marginTop,
                                        graph.VisibleClipBounds.Width * (marginLeft - marginArrow), graph.VisibleClipBounds.Height * (marginTop + marginArrow));
            graph.DrawLine(sBlackPen, graph.VisibleClipBounds.Width * marginLeft, graph.VisibleClipBounds.Height * marginTop,
                                        graph.VisibleClipBounds.Width * (marginLeft + marginArrow), graph.VisibleClipBounds.Height * (marginTop + marginArrow));

            // Y axis
            graph.DrawLine(sBlackPen, graph.VisibleClipBounds.Width * marginLeft, graph.VisibleClipBounds.Height / 2,
                                        graph.VisibleClipBounds.Width * marginRight, graph.VisibleClipBounds.Height / 2);
            // Arrow
            graph.DrawLine(sBlackPen, graph.VisibleClipBounds.Width * marginRight, graph.VisibleClipBounds.Height / 2,
                                        graph.VisibleClipBounds.Width * (marginRight - marginArrow), (graph.VisibleClipBounds.Height / 2) + graph.VisibleClipBounds.Height * marginArrow);
            graph.DrawLine(sBlackPen, graph.VisibleClipBounds.Width * marginRight, graph.VisibleClipBounds.Height / 2,
                                        graph.VisibleClipBounds.Width * (marginRight - marginArrow), (graph.VisibleClipBounds.Height / 2) - graph.VisibleClipBounds.Height * marginArrow);

        }

        public static void SetResizeRedraw(Control control)
        {
            typeof(Control).InvokeMember("ResizeAll",
              BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
              null, control, new object[] { true });
        }
    }
}
